package org.dey.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dey.constant.MessageConstant;
import org.dey.entity.Result;
import org.dey.mapper.RoleMapper;
import org.dey.pojo.PageVo;
import org.dey.pojo.Role;
import org.dey.service.RoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author deyran
 * @date 2022/3/15 9:12
 * @classname
 * @see
 */
@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Override
    public PageVo<Role> queryrole(Role role, int pageno, int pagesize) {
        //组装分页
        IPage<Role> page = new Page<Role>(pageno,pagesize);

        //组装条件
        QueryWrapper<Role> queryWrapper = new QueryWrapper<Role>();

        //根据名字模糊查询
        if(StringUtils.isNotBlank(role.getName())) {
            queryWrapper.like("name", role.getName());
        }

        IPage<Role> pageinfo = this.page(page,queryWrapper);

        PageVo<Role> pageVo =new PageVo<Role>();
        pageVo.setRows(pageinfo.getRecords());
        pageVo.setTotal(pageinfo.getTotal());
        return pageVo;
    }

    @Override
    public Result insertrole(Role role) {
        Result result = null;

        boolean one = this.save(role);

        if(one){
            result = new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        }else {
            result = new Result(true, MessageConstant.ADD_ROLE_FAIL);
        }

        return result;
    }

    @Override
    public Result delrole(int id) {
        Result result = null;
        boolean b = this.removeById(id);
        if(b){
            result = new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        }else {
            result = new Result(true, MessageConstant.DELETE_ROLE_FAIL);
        }
        return result;
    }

    @Override
    public Role queryrolebyid(int id) {
        return this.getById(id);
    }

    @Override
    public Result updaterolebyid(Role role) {
        Result result = null;
        boolean b = this.updateById(role);
        if(b){
            result = new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        }else {
            result = new Result(true, MessageConstant.EDIT_ROLE_FAIL);
        }
        return result;
    }

    @Override
    public List<Role> roleAll() {
        List<Role> list = this.list();

        return list;
    }
}
